/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package hyper.market.beans;

import database.DBAccessController;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 *
 * @author Mikeno
 */
public class StaffBean {
    private String loginName, password;
    private int staffid, storeid;
    private int manager, contact;
    private String joinedDate,role;

    public StaffBean(String name, String password){
        this.setLoginName(name);
        this.setPassword(password);
    }
    public StaffBean(){

    }

    public int authenticateUser(String jndi){
        DBAccessController db = new DBAccessController(jndi);
        String sql = "select staffid, role , storeid from staff where loginName = '"+getLoginName()+"' and password = '"+getPassword()+"'";

          try{
  		ResultSet rs = db.execSelectQuery(sql);
  		if (rs.next()){
  			this.setStaffid(rs.getInt(1));
                        this.setRole(rs.getString(2));
                        this.setStoreid(rs.getInt(3));
  		}
  		db.terminate();
  	}catch (SQLException e) {
  	       e.printStackTrace();
  	       return 0;
  	}

        // 1 - Manager, 2 - Admin, 0 - Error.
        if(getRole().equals("Manager")){
            return 1;
        }else if(getRole().equals("Admin")){
            return 2;
        }else{
            return 0;
        }
    }
    
    public ArrayList retrieveStoreWorkers(int storeid,String jndi){

        if(storeid == 0){
            return null;
        }
        DBAccessController db = new DBAccessController(jndi);
        String sql = "select StaffId, LoginName, joinedDate, Role,contactNo from staff where storeid = '"+storeid+"' Order By Role DESC";
        ArrayList list = new ArrayList();
        StaffBean staff = null;
        DateFormat df = new SimpleDateFormat("dd MMM yyyy");

          try{
  		ResultSet rs = db.execSelectQuery(sql);
  		while (rs.next()){
                        staff = new StaffBean();
  			staff.setStaffid(rs.getInt(1));
                        staff.setLoginName(rs.getString(2));
                        staff.setJoinedDate(df.format(rs.getDate(3)));
                        staff.setRole(rs.getString(4));
                        staff.setContact(rs.getInt(5));
                        list.add(staff);
  		}
  		db.terminate();
  	}catch (SQLException e) {
  	       e.printStackTrace();
  	     return null;
  	}
        return list;
    }


/*
    public void testinsert(String jn){
        DBAccessController db = new DBAccessController(jn);
        String sql = "insert into stock (stockname, quantity) values('sexy', 5)";

            db.execUpdateQuery(sql);
            db.terminate();
  	
    }
 */

    /**
     * @return the loginName
     */
    public String getLoginName() {
        return loginName;
    }

    /**
     * @param loginName the loginName to set
     */
    public void setLoginName(String loginName) {
        this.loginName = loginName;
    }

    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }

    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * @return the staffid
     */
    public int getStaffid() {
        return staffid;
    }

    /**
     * @param staffid the staffid to set
     */
    public void setStaffid(int staffid) {
        this.staffid = staffid;
    }

    /**
     * @return the storeid
     */
    public int getStoreid() {
        return storeid;
    }

    /**
     * @param storeid the storeid to set
     */
    public void setStoreid(int storeid) {
        this.storeid = storeid;
    }

    /**
     * @return the manager
     */
    public int getManager() {
        return manager;
    }

    /**
     * @param manager the manager to set
     */
    public void setManager(int manager) {
        this.manager = manager;
    }

    /**
     * @return the joinedDate
     */
    public String getJoinedDate() {
        return joinedDate;
    }

    /**
     * @param joinedDate the joinedDate to set
     */
    public void setJoinedDate(String joinedDate) {
        this.joinedDate = joinedDate;
    }

    /**
     * @return the contact
     */
    public int getContact() {
        return contact;
    }

    /**
     * @param contact the contact to set
     */
    public void setContact(int contact) {
        this.contact = contact;
    }

    /**
     * @return the role
     */
    public String getRole() {
        return role;
    }

    /**
     * @param role the role to set
     */
    public void setRole(String role) {
        this.role = role;
    }

}
